import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _isString from "lodash/isString";
// @ts-ignore
import dynamic from 'dva/dynamic'; // @ts-ignore

import { createElement } from 'react';
import React, { useEffect, useRef, useState } from 'react';
export var Vue2ReactWrapper = function Vue2ReactWrapper(vueComponentFactory, componentName) {
  var ReactPage = function ReactPage() {
    var _useState = useState(true),
        _useState2 = _slicedToArray(_useState, 2),
        loading = _useState2[0],
        setLoading = _useState2[1];

    var _useState3 = useState(''),
        _useState4 = _slicedToArray(_useState3, 2),
        errMsg = _useState4[0],
        setErrMsg = _useState4[1];

    var containerRef = useRef(null);
    useEffect(function () {
      var Vue = require('vue');

      if (Vue && Vue.default) {
        Vue = Vue.default;
      }

      if (!Vue) {
        setLoading(false);
        setErrMsg('window.Vue 为空, 可以添加 vue cdn 解决此问题: https://www.bootcdn.cn/vue/');
        return;
      }

      if (containerRef.current) {
        var domRef = containerRef.current;
        var vueInstance;

        _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
          var vueApp;
          return _regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  _context.next = 2;
                  return vueComponentFactory();

                case 2:
                  vueApp = _context.sent;
                  vueApp = vueApp.default || vueApp; // const { createApp } = Vue;
                  // vueInstance = createApp(vueApp).mount(domRef);

                  vueInstance = new Vue({
                    render: function render(h) {
                      return h(vueApp);
                    }
                  });
                  vueInstance.$mount(containerRef.current);
                  setLoading(false);

                case 7:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee);
        }))();

        return function () {
          if (!vueInstance) {
            return;
          }

          if (!Vue) {
            return;
          }

          if (vueInstance.$destroy) {
            vueInstance.$destroy();
          } else if (vueInstance.unmount) {
            vueInstance.unmount();
          } else {
            Vue.render(null, domRef);
          }
        };
      }
    }, []);
    return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
      ref: containerRef
    }), loading && /*#__PURE__*/React.createElement("div", null, " loading "), errMsg && /*#__PURE__*/React.createElement("div", null, " ", errMsg, " "));
  };

  ReactPage.displayName = componentName;
  return ReactPage;
};
export function getConvertRouter(_ref2) {
  var cpaasRoutes = _ref2.cpaasRoutes,
      _ref2$getModels = _ref2.getModels,
      getModels = _ref2$getModels === void 0 ? function (e) {
    return e;
  } : _ref2$getModels,
      _ref2$getPages = _ref2.getPages,
      getPages = _ref2$getPages === void 0 ? function (e) {
    return e;
  } : _ref2$getPages,
      options = _ref2.options;

  var modelNotExisted = function modelNotExisted() {
    var app = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    var model = arguments.length > 1 ? arguments[1] : undefined;
    return (// eslint-disable-next-line
      !(app._models || []).some(function (_ref3) {
        var namespace = _ref3.namespace;
        return namespace === model.substring(model.lastIndexOf('/') + 1);
      })
    );
  };

  function unWindRouteToRouter(cpaasRoutes, convertRoutes) {
    var ndeep = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
    cpaasRoutes.forEach(function (route, index) {
      var newRoute = _objectSpread({}, route);

      if (ndeep === 1) {
        newRoute.index = index === 0;
      }

      assignRouterData(newRoute, route); // eslint-disable-next-line no-param-reassign

      convertRoutes[route.path] = newRoute; // 子路由

      if (route.components !== undefined) {
        unWindRouteToRouter(route.components, convertRoutes, ndeep + 1);
        newRoute.notTrueRoute = true;
      }
    });
  }

  function assignRouterData(router, route) {
    var app = options.app;
    var models = route.models === undefined ? [] : route.models;

    if (route.component === undefined) {
      // @ts-ignore eslint-disable-next-line no-param-reassign
      router.component = require('cpaas-front/lib/routes').default;
    } else {
      var getRouterData = _isString(route.component) ? function () {
        return getPages(route.component);
      } : route.component;

      switch (route.type) {
        case 'vue':
          // eslint-disable-next-line no-param-reassign
          router.component = Vue2ReactWrapper(getRouterData, route.component);
          break;

        case 'react':
        default:
          // eslint-disable-next-line no-param-reassign
          router.component = dynamicWrapper(app, models, getRouterData);
          break;
      }
    }

    if (route.key !== undefined) {
      // eslint-disable-next-line no-param-reassign
      router.key = route.key;
    }

    if (route.authorized !== undefined) {
      // eslint-disable-next-line no-param-reassign
      router.authorized = route.authorized;
    }

    if (route.title !== undefined) {
      // eslint-disable-next-line no-param-reassign
      router.title = route.title;
    }

    if (route.icon !== undefined) {
      // eslint-disable-next-line no-param-reassign
      router.icon = route.icon;
    } // eslint-disable-next-line no-param-reassign


    router.exact = route.exact;
  } // wrapper of dynamic


  var dynamicWrapper = function dynamicWrapper(app) {
    var _models = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];

    var _component = arguments.length > 2 ? arguments[2] : undefined;

    return dynamic({
      app: app,
      models: function models() {
        return _models.filter(function (item) {
          if (_isString(item) && !modelNotExisted(app, item)) {
            return false;
          } else {
            return true;
          }
        }).map(function (item) {
          return _isString(item) ? getModels(item) : typeof item === 'function' ? item() : item;
        });
      },
      // add routerData prop
      component: function component() {
        // if (!routerDataCache) {
        //   routerDataCache = getRouterData(app);
        // }
        return _component().then(function (raw) {
          var Component = raw.default || raw;
          return function (props) {
            return createElement(Component, _objectSpread({}, props));
          };
        });
      }
    });
  };

  function convertRouter() {
    var convertRoutes = {};
    unWindRouteToRouter(cpaasRoutes, convertRoutes);
    return convertRoutes;
  }

  return convertRouter;
}
